package com.ruoyi.system.service.impl.processServiceImpl;


import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.process.ProcessAluminized;
import com.ruoyi.system.domain.process.ProcessAluminizedInventory;
import com.ruoyi.system.domain.process.ProcessAluminizedRibbon;
import com.ruoyi.system.domain.process.ProcessInventory;
import com.ruoyi.system.mapper.process.ProcessAluminizedInventoryMapper;
import com.ruoyi.system.mapper.process.ProcessAluminizedMapper;
import com.ruoyi.system.mapper.process.ProcessAluminizedRibbonMapper;
import com.ruoyi.system.mapper.process.ProcessInventoryMapper;
import com.ruoyi.system.service.processService.IProcessAluminizedService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 加工镀铝Service业务层处理
 * 
 * @author ljx
 * @date 2020-07-09
 */
@Service
public class ProcessAluminizedServiceImpl implements IProcessAluminizedService
{
    @Resource
    private ProcessAluminizedMapper processAluminizedMapper;
    @Resource
    private ProcessAluminizedInventoryMapper processAluminizedInventoryMapper;
    @Resource
    private ProcessInventoryMapper processInventoryMapper;
    @Resource
    private ProcessAluminizedRibbonMapper processAluminizedRibbonMapper;

    /**
     * 查询加工镀铝
     * 
     * @param id 加工镀铝ID
     * @return 加工镀铝
     */
    @Override
    public ProcessAluminized selectProcessAluminizedById(Long id)
    {
        return processAluminizedMapper.selectProcessAluminizedById(id);
    }

    /**
     * 查询加工镀铝列表
     * 
     * @param processAluminized 加工镀铝
     * @return 加工镀铝
     */
    @Override
    public List<ProcessAluminized> selectProcessAluminizedList(ProcessAluminized processAluminized)
    {
        return processAluminizedMapper.selectList(processAluminized);
    }

    /**
     * 新增加工镀铝
     * 
     * @param processAluminized 加工镀铝
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProcessAluminized(ProcessAluminized processAluminized)
    {
        processAluminized.setCreateTime(DateUtils.getNowDate());
        //插入镀铝表获得id
        processAluminizedMapper.insertProcessAluminized(processAluminized);
        this.add(processAluminized);
        return 1;
    }

    public void add(ProcessAluminized processAluminized){
        //插入镀铝和并卷关联表数据
        if(processAluminized.getRibbonId()!=null){
            ProcessAluminizedRibbon aluminizedRibbon = new ProcessAluminizedRibbon();
            aluminizedRibbon.setRibbonId(processAluminized.getRibbonId());
            aluminizedRibbon.setAluminizedId(processAluminized.getId());
            processAluminizedRibbonMapper.insertProcessAluminizedRibbon(aluminizedRibbon);
        }
        //添加镀铝和原膜编号关联表数据
        if(StringUtils.isNotEmpty(processAluminized.getInventoryIds())){
            List<String> list = Arrays.asList(processAluminized.getInventoryIds().split(","))
                    .stream().distinct().collect(Collectors.toList());
            ProcessAluminizedInventory aluminizedInventory = new ProcessAluminizedInventory();
            aluminizedInventory.setAluminizedId(processAluminized.getId());
            //插入并卷和加工库存关联表
            processAluminizedInventoryMapper.insertList(processAluminized.getId(),list);
            //修改加工库存表的加工状态
            processInventoryMapper.updateStatusString(3,list);
        }
    }

    /**
     * 修改加工镀铝
     * 
     * @param processAluminized 加工镀铝
     * @return 结果
     */
    @Override
    public int updateProcessAluminized(ProcessAluminized processAluminized)
    {
        processAluminized.setUpdateTime(DateUtils.getNowDate());
        if(processAluminized.getSign()== 2){
            //查询到历史绑定原膜编号，修改原膜编号加工状态
            List<ProcessInventory> inventories = processInventoryMapper.selectInventoryByAluminizedId(processAluminized.getId());
            List<Long> list = inventories.stream().map(ProcessInventory::getId).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(list)){
                processInventoryMapper.updateStatus(2,list);
            }
            //删除关联表数据
            String[] de = new String[]{String.valueOf(processAluminized.getId())};
            //删除镀铝和并卷关联表数据
            processAluminizedRibbonMapper.deleteByRibbonIds(de);
            //删除镀铝和原膜编号关联表数据
            processAluminizedInventoryMapper.deleteInventoryByIds(de);
            //添加镀铝和并卷关联表数据
            //添加镀铝和原膜编号关联表数据
            //修改原膜编号加工状态
            this.add(processAluminized);
        }
        //sign==1时只修改基础信息
        return processAluminizedMapper.updateProcessAluminized(processAluminized);
    }

    /**
     * 删除加工镀铝对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public AjaxResult deleteProcessAluminizedByIds(String ids)
    {
        String[] array = Convert.toStrArray(ids);
        Boolean flag = false;
        List<String> delete = new ArrayList<>();
        ProcessAluminizedInventory aluminizedInventory = new ProcessAluminizedInventory();
        for (String s : array) {
            Long aLong = Long.valueOf(s);
            aluminizedInventory.setAluminizedId(aLong);
            //查询出镀铝和原膜关系表数据
            List<ProcessInventory> inventories = processInventoryMapper.selectInventoryByAluminizedId(aLong);
            List<ProcessInventory> collect = inventories.stream().filter(p -> p.getStatus() > 3).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(collect)){
                //说明该镀铝绑定的原膜加工状态为大于镀铝
                flag = true;
                continue;
            }
            delete.add(s);
            //修改原膜编号加工状态
            List<Long> list = inventories.stream().map(ProcessInventory::getId).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(list)){
                processInventoryMapper.updateStatus(2,list);
            }
        }
        if(CollectionUtils.isNotEmpty(delete)){
            //删除关联表数据
            String[] de = new String[delete.size()];
            de = delete.toArray(de);
            //删除镀铝和并卷关联表数据
            processAluminizedRibbonMapper.deleteByRibbonIds(de);
            //删除镀铝和原膜编号关联表数据
            processAluminizedInventoryMapper.deleteInventoryByIds(de);
            //删除镀铝表数据
            processAluminizedMapper.deleteProcessAluminizedByIds(de);
        }
        if(flag){
            return AjaxResult.success("原膜已加工无法删除");
        }
        return AjaxResult.success();
    }

    /**
     * 删除加工镀铝信息
     * 
     * @param id 加工镀铝ID
     * @return 结果
     */
    @Override
    public int deleteProcessAluminizedById(Long id)
    {
        return processAluminizedMapper.deleteProcessAluminizedById(id);
    }

    @Override
    public List<ProcessInventory> getProcessInventory(Long id) {
        return processInventoryMapper.selectInventoryByAluminizedId(id);
    }
}
